<?php
namespace Web\Logic;
class MenuLogic {

	/**
	 * 保存数据
	 * @param unknown $data
	 */
	function updataSort($data){
		//返回值
		$istrue = false;
		//数据判断
		if($data){
			//数据处理
			$udata = $this->clData($data);
			//数据修改
			foreach ($udata as $u){
				$istrue = $this->updataSortSql($u);
			}
		}
		return $istrue;
	}

    /**
     * 数据处理
     */
	function clData($data){
		//返回值
		$rdata = array();
		//判断
		if($data){
			foreach ($data as $k => $v){
				$r['menuid'] = $k;
				$r['sorts'] = $v;
				$rdata[] = $r;
			}
		}
		return $rdata;
	}

	/**
	 * 保存数据
	 * @param unknown $data
	 */
	function updataSortSql($data){
		try {
			//实例化模块
    		$mod = M("menu");
    		$udata['sorts'] = $data['sorts'];
    		return $mod->where("menuid = '$data[menuid]'")->save($udata);
		} catch (Exception $e) {
		}
	}

    /**
     * 添加数据
     * @param unknown $data
     */
    function addDataSql($data){
        try {
            //实例化模块
            $mod = M("menu");
            return $mod->data($data)->add();
        } catch (Exception $e) {
        }
    }

    /**
     * @desc 查询导航栏
     * @param $id 上级id
     * @param $roleid  栏目权限
     */
    function getDhl($id,$roleid=0)
    {
        //实例化模块
        $mod = M("menu");
        if($roleid){
            $where = " and menuid in ($roleid) ";
        }
        //获取父栏目id
        if($id >0)
            $wdata = "parentid = $id".$where;
        else
            $wdata = "(parentid = 0 or parentid is null)".$where;
        $listrow =  $mod
        ->where($wdata)
        ->field("menuid,menuname,url,parentid,sorts
    			    ,case when sorts is null then 99
                    when sorts >0 then sorts end as sorte")
        ->order('sorte asc')
        ->select();
        //return $wdata;
        return $listrow;
    }

    /***
     * 删除
     * @param unknown $id
     * @return Ambigous <mixed, boolean, unknown>
     */
    function delSql($id){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		return $mod->where("menuid = $id")->delete();
    	} catch (Exception $e) {
    	}
    }

    /***
     * 保存
     * @param unknown $id
     * @return Ambigous <mixed, boolean, unknown>
     */
    function saveDataSql($id,$data){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		return $mod->where("menuid = '$id'")->save($data);
    	} catch (Exception $e) {
    	}
    }

    /**
     * 查询一条数据
     * @param unknown $id
     * @return Ambigous <mixed, boolean, NULL, string, unknown, multitype:, object>
     */
    function selOneSql($id){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		//查询信息
    		$listRows = $mod->where("menuid = '$id'")
    		->field("menuid,menuname,url,parentid")
    		->find();
    		return $listRows;
    	} catch (Exception $e) {
    	}
    }

    /**
     * 查询子栏目数据
     * @param unknown $id
     * * @param 权限栏目 $roleid
     * @return Ambigous <mixed, boolean, NULL, string, unknown, multitype:, object>
     */
    function selZmenuSql($id,$roleid){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		//查询条件
    		$whers = "parentid = '$id'";
    		if($roleid){
    			$whers .= " and menuid in ($roleid) ";
    		}
    		//查询信息
    		$listRows = $mod->where($whers)
    		->field("menuid,menuname,url,parentid,sorts,oico
    			    ,case when sorts is null then 99
                    when sorts >0 then sorts end as sorte")
            ->order('sorte ASC')
    		->select();
    		return $listRows;
    	} catch (Exception $e) {
    	}
    }


    /**
     * 查询总条数
     * @param unknown $wdata
     */
    function selNum($wdata){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		return $mod->field("menuid")->where($wdata)->count();
    	} catch (Exception $e) {
    	}
    }

    /**
     * 查询数据
     * @param unknown $wdata
     * @param unknown $Page
     */
    function indexList($wdata,$Page){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		$rdata = $mod->field("menuid,menuname,url,updatetimes,sorts
    				             ,case when sorts is null then 99
                                   when sorts >0 then sorts end as sorte")
                    ->where($wdata)
                    ->order('sorte ASC')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->select();
    		//查询子菜单数量
    		$nLeg = count($rdata);
    		for($i=0; $i<$nLeg; $i++)
    		{
    			$rdata[$i]['counts'] = $this->getChdCount($rdata[$i]['menuid']);
    		}
    		return $rdata;
    	} catch (Exception $e) {
    	}
    }

      /**
     * @desc 查询子栏目个数
     *
     */
    private function getChdCount($id)
    {
        //实例化模块
        $mod = M("menu");
        $listrow =  $mod
        ->where("parentid = '$id'")
        ->field("menuid")
        ->count();
        return $listrow;
    }






}